/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Controller.CustomerManager;
import Controller.LogManager;
import java.io.IOException;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author pauld
 */
@WebServlet(name = "changePW", urlPatterns = {"/changePW"})
public class changePW extends HttpServlet{
    private static String oldPW = null,
            newPW1 = null,
            newPW2 = null;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        response.setContentType("text/html;charset=UTF-8");
            
        int result = CustomerManager.changePassword(oldPW, newPW1, request.getSession());
        
        switch(result){
            case 1:
                //changed pw successfully
                LogManager.logEvent("ChangePW." + Thread.currentThread().getStackTrace()[1].getMethodName(), Level.INFO, "Change Password Successful", request.getRemoteAddr(), String.valueOf(request.getSession().getAttribute("name")));
                response.sendRedirect("myProfile.jsp?saved=3");
                break;
            case 0:
                //old password did not match
                LogManager.logEvent("ChangePW." + Thread.currentThread().getStackTrace()[1].getMethodName(), Level.WARNING, "Change Password Unsuccessful - Passwords Do Not Match", request.getRemoteAddr(), String.valueOf(request.getSession().getAttribute("name")));
                response.sendRedirect("myProfile.jsp?saved=4");
                break;
            case -2:
                //problem with db
                LogManager.logEvent("ChangePW." + Thread.currentThread().getStackTrace()[1].getMethodName(), Level.SEVERE, "Change Password Unsuccessful - Databse Error", request.getRemoteAddr(), String.valueOf(request.getSession().getAttribute("name")));
                response.sendRedirect("myProfile.jsp?saved=2");
                break;
        }
        
    }
    
    private static boolean checkInput(HttpServletRequest request){
        oldPW = request.getParameter("oldPW");
        newPW1 = request.getParameter("newPW1");
        newPW2 = request.getParameter("newPW2");
        
        String regexAlphaNum = "^[A-Za-z0-9]+$";
        Pattern pAlphaNum = Pattern.compile(regexAlphaNum, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
        Matcher mOldPW = pAlphaNum.matcher(oldPW),
                mNewPW1 = pAlphaNum.matcher(newPW1),
                mNewPW2 = pAlphaNum.matcher(newPW2);
        boolean resultOldPW = mOldPW.find(),
                resultNewPW1 = mNewPW1.find(),
                resultNewPW2 = mNewPW2.find();
        
        if( (newPW1.equals(newPW2)) &&
            (newPW1.length() >= 8 && newPW1.length() <= 25) &&
            (newPW2.length() >= 8 && newPW2.length() <= 25) &&
            (oldPW.length() >= 1 && oldPW.length() <= 25) &&
                resultOldPW &&
                resultNewPW1 &&
                resultNewPW2){
            return true;
        }
        else{
            LogManager.logEvent("ChangePW." + Thread.currentThread().getStackTrace()[1].getMethodName(), Level.WARNING, "Change Password Unsuccessful - Invalid Input", request.getRemoteAddr(), String.valueOf(request.getSession().getAttribute("name")));
            return false;
        }
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if( checkInput(request) )
            processRequest(request, response);
        else
            response.sendRedirect("myProfile.jsp?saved=5");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if( checkInput(request) )
            processRequest(request, response);
        else
            // new passwords did not match
            response.sendRedirect("myProfile.jsp?saved=5");
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
